#include "ennemi.h"

Ennemi::Ennemi(Jeu* jeu) : jeu(jeu)
{
    spritecompteur = 0;
    //hp = vitalite();
    jeu->getEnnemis().append(this);
    qDebug() << jeu->getEnnemis().size();
    jeu->getPlateau().scene()->addItem(this);
    setPos(jeu->getDepart().x()*128,jeu->getDepart().y()*128);
}
void Ennemi::touche(float frappe)
{
    qDebug() << "ouch : " << frappe-resistance();
    if (frappe-resistance() > 0) hp -= frappe-resistance();
}
void Ennemi::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{

    painter->setBrush(Qt::white);
    painter->drawRect(-64,-64,128,20);
    if (hp >= 0)
    {
        painter->setBrush(Qt::green);
        painter->drawRect(-64,-64,(hp/vitalite())*128,20);
    }
    int delai = 4;
    int nbsprites = getSprites().size();
    int sprite = spritecompteur/delai;
    if (sprite >= nbsprites) sprite = (nbsprites-1)*2-sprite; // 0 1 2 1 0 1 2 1 0 ... au lieu de 0 1 2 3 0 1 2 3 ...
    painter->drawPixmap(-taille*64,-taille*64,taille*128,taille*128,getSprites().at(sprite));
    if (nbsprites > 1)
    {
      spritecompteur++;
      spritecompteur = spritecompteur % (delai*(nbsprites-1)*2);
    }

}
QRectF Ennemi::boundingRect() const
{
    return QRectF(-taille*64, -taille*64, taille*128,taille*128);
}
void Ennemi::advance(int phase)
{
    if (phase == 1)
    {
        //qDebug() << "coord : " << y() << "," << x();
        //qDebug() << "case : " << round(y()/128) << "," << round(x()/128);

        Case& c = *jeu->getMap()[round(y()/128)][round(x()/128)];

        setRotation(c.getRotation());
        float mvt = 128*vitesse()/50.0;
        if (c.getRotation()%90 != 0) mvt *= sqrt(0.5);
        if (c.getNord()) moveBy(0,-mvt);
        if (c.getEst()) moveBy(mvt,0);
        if (c.getOuest()) moveBy(-mvt,0);
        if (c.getSud()) moveBy(0,mvt);


        //qDebug() << "advance !";

        /*
        QList<QGraphicsItem*> objets = scene()->items();
        for (int i = 0; i < objets.size(); i++)
        {

            if (Case* c = dynamic_cast<Case*>(objets[i]))
            {

               // qDebug() << "collision avec une case " << i << "! rotation = " << c->getRotation();
                setRotation(c->getRotation());

                float mvt = 1;
                if (c->getRotation()%90 != 0) mvt *= sqrt(0.5);
                if (c->getNord()) moveBy(0,-mvt);
                if (c->getEst()) moveBy(mvt,0);
                if (c->getOuest()) moveBy(-mvt,0);
                if (c->getSud()) moveBy(0,mvt);
            }

            // Permet de trouver tous les ennemis dans la meme scene
             if (Ennemi* e = dynamic_cast<Ennemi*>(objets[i]))
             {
                 //qDebug() << "ennemi[" << i << "].x = " << e->x();
             }
        }
        */
    }
}

void Ennemi::avancer(int h, int v)
{
    if (h > 0) setRotation(0);
    if (h < 0) setRotation(180);
    if (v > 0) setRotation(90);
    if (v < 0) setRotation(-90);

    if (h > 0 && v < 0) setRotation(-45);
    if (h > 0 && v > 0) setRotation(45);
    if (h < 0 && v < 0) setRotation(-135);
    if (h < 0 && v > 0) setRotation(135);

    float xx = h*0.128; // 128/1000
    float yy = v*0.128; //
    if ((int)rotation()%90 != 0)
    {
        xx *= sqrt(0.5);
        yy *= sqrt(0.5);
    }
    int oldx = round(x()/128);
    int oldy = round(y()/128);
    setPos(x()+xx,y()+yy);
    int newx = round(x()/128);
    int newy = round(y()/128);
    if (oldx != newx || oldy != newy)
    {

        //qDebug() << "nouvelle case : " << QTime::currentTime().second() << ":" << QTime::currentTime().msec();
    }
    //setRotation(rotation()+10);
    //qDebug() << "avancer : " << x() << "," << y();


}
